package com.springtest.Controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

//import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;

import com.hnair.OdsConsumer;
import com.hnair.QueryMbrInfo;

import org.jasig.cas.client.authentication.AttributePrincipal;

import java.util.Map;

@Controller
@RequestMapping("view/test/*")
public class login {
	private static Logger logger = Logger.getLogger(login.class);  
	
	@RequestMapping("login.do")
	public String DefaultLogin(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		request.setCharacterEncoding("utf-8");
		String UserName = request.getParameter("UserName");
		String Passwd = request.getParameter("UserPasswd");
		HttpSession session = request.getSession();
		session.setAttribute("UserName", UserName);
		session.setAttribute("UserPasswd", Passwd);
		session.setAttribute("UserType", "test");
		
//		response.setCharacterEncoding("utf-8");
//		response.setHeader("Content-type", "text/html;charset=UTF-8");
//		response.getWriter().print(UserName + ":" + Passwd);
//		response.sendRedirect("/SpringTest/view/Success.jsp");
//		response.getWriter().flush();
//		response.getWriter().close();
		logger.debug("tiles.page");
		response.sendRedirect("/SpringTest/view/Success.jsp");
		return null;
	}
	
	
	@RequestMapping("ffpsearch.do")
	public String FfpSearch(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		request.setCharacterEncoding("utf-8");
		String MobileNo = request.getParameter("MobileNo");	
		logger.debug(MobileNo);
		QueryMbrInfo Qry = new QueryMbrInfo();
		String MbrInfo = Qry.TestSerchByMobile(MobileNo);
//		String MbrInfo = "[{\"username\": \"lele\", \"type\":  \"sss\"}]";
		logger.debug(MbrInfo);
		JSONObject jsonObject = new JSONObject(MbrInfo);
//		JSONArray resultArray = jsonObject.getJSONArray("errorCode");
		String ErrorCode = jsonObject.getString("errorCode");
		if(!ErrorCode.equalsIgnoreCase("0"))
		{
			logger.debug(ErrorCode);
			String ErrMsg = "<p>" + jsonObject.getString("errorMessage") + "</p>";
			logger.debug(ErrMsg);
			response.getWriter().print(ErrMsg);
			response.getWriter().flush();
			response.getWriter().close();
			return null;
		}
		JSONArray resultArray = jsonObject.getJSONArray("owners");
		if(resultArray.length() == 0)
		{
			response.getWriter().print("<p> " + MobileNo +" not found</p>");
			response.getWriter().flush();
			response.getWriter().close();
			return null;
		}
		//String JsonName = resultArray.getJSONObject(0).getString("username");
		String CID = resultArray.getJSONObject(0).getString("cid");
		
		String Detail = Qry.TestSerchByCID(CID);
		logger.debug(Detail);
		JSONObject DetailObj = new JSONObject(Detail);
		ErrorCode = DetailObj.getString("errorCode");
		if(!ErrorCode.equalsIgnoreCase("0"))
		{
			logger.debug(ErrorCode);
			String ErrMsg = "<p>" + jsonObject.getString("errorMessage") + "</p>";
			logger.debug(ErrMsg);
			response.getWriter().print(ErrMsg);
			response.getWriter().flush();
			response.getWriter().close();
			return null;
		}
//		JSONArray DetailArray = jsonObject.getJSONArray("owners");
//		if(DetailArray.length() == 0)
//		{
//			response.getWriter().print("<p> CID" + MobileNo +" not found</p>");
//			response.getWriter().flush();
//			response.getWriter().close();
//			return null;
//		}
		String Result = "";
		Result += String.format("<p>birthday:%s</p><br/>", DetailObj.getString("birthday"));
		Result += String.format("<p>cid:%s</p><br/>", DetailObj.getString("cid"));
		Result += String.format("<p>nationality:%s</p><br/>", DetailObj.getString("nationality"));
		Result += String.format("<p>id:%s</p><br/>", DetailObj.getString("id"));
		Result += String.format("<p>pass:%s</p><br/>", DetailObj.getString("pass"));
		Result += String.format("<p>mobile:%s</p><br/>", DetailObj.getString("mobile"));
		Result += String.format("<p>mobileStatus:%s</p><br/>", DetailObj.getString("mobileStatus"));
		Result += String.format("<p>email:%s</p><br/>", DetailObj.getString("email"));
		Result += String.format("<p>emailStatus:%s</p><br/>", DetailObj.getString("emailStatus"));
		Result += String.format("<p>title:%s</p><br/>", DetailObj.getString("title"));
		Result += String.format("<p>gender:%s</p><br/>", DetailObj.getString("gender"));
		Result += String.format("<p>lastNameEn:%s</p><br/>", DetailObj.getString("lastNameEn"));
		Result += String.format("<p>firstNameEn:%s</p><br/>", DetailObj.getString("firstNameEn"));
		Result += String.format("<p>lastNameZh:%s</p><br/>", DetailObj.getString("lastNameZh"));
		Result += String.format("<p>firstNameZh:%s</p><br/>", DetailObj.getString("firstNameZh"));
		Result += String.format("<p>status:%s</p><br/>", DetailObj.getString("status"));
		Result += String.format("<p>language:%s</p><br/>", DetailObj.getString("language"));	
		
		response.setCharacterEncoding("utf-8");
		//response.setHeader("Content-type", "text;charset=UTF-8");
		response.getWriter().print(Result);
		response.getWriter().flush();
		response.getWriter().close();
		logger.debug("run ffpsearch.do");
		return null;
	}
	
	@RequestMapping("FfpIDSearch.do")
	public String FfpIDSearch(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		request.setCharacterEncoding("utf-8");
		String IDNO = request.getParameter("identify_no");	
		logger.debug(IDNO);
		
		QueryMbrInfo Qry = new QueryMbrInfo();
		String MbrInfo = Qry.TestSerchByIDNO(IDNO);
//		String MbrInfo = "[{\"username\": \"lele\", \"type\":  \"sss\"}]";
		logger.debug(MbrInfo);
		JSONObject jsonObject = new JSONObject(MbrInfo);
//		JSONArray resultArray = jsonObject.getJSONArray("errorCode");
		String ErrorCode = jsonObject.getString("errorCode");
		if(!ErrorCode.equalsIgnoreCase("0"))
		{
			logger.debug(ErrorCode);
			String ErrMsg = "<p>" + jsonObject.getString("errorMessage") + "</p>";
			logger.debug(ErrMsg);
			response.getWriter().print(ErrMsg);
			response.getWriter().flush();
			response.getWriter().close();
			return null;
		}
		JSONArray resultArray = jsonObject.getJSONArray("memberAllId");
		if(resultArray.length() == 0)
		{
			response.getWriter().print("<p> " + IDNO +" not found</p>");
			response.getWriter().flush();
			response.getWriter().close();
			return null;
		}
		//String JsonName = resultArray.getJSONObject(0).getString("username");
		String CID = resultArray.getJSONObject(0).getString("cid");
		
		String Detail = Qry.TestSerchByCID(CID);
		logger.debug(Detail);
		JSONObject DetailObj = new JSONObject(Detail);
		ErrorCode = DetailObj.getString("errorCode");
		if(!ErrorCode.equalsIgnoreCase("0"))
		{
			logger.debug(ErrorCode);
			String ErrMsg = "<p>" + jsonObject.getString("errorMessage") + "</p>";
			logger.debug(ErrMsg);
			response.getWriter().print(ErrMsg);
			response.getWriter().flush();
			response.getWriter().close();
			return null;
		}
//		JSONArray DetailArray = jsonObject.getJSONArray("owners");
//		if(DetailArray.length() == 0)
//		{
//			response.getWriter().print("<p> CID" + MobileNo +" not found</p>");
//			response.getWriter().flush();
//			response.getWriter().close();
//			return null;
//		}
		String Result = "";
		Result += String.format("<p>birthday:%s</p><br/>", DetailObj.getString("birthday"));
		Result += String.format("<p>cid:%s</p><br/>", DetailObj.getString("cid"));
		Result += String.format("<p>nationality:%s</p><br/>", DetailObj.getString("nationality"));
		Result += String.format("<p>id:%s</p><br/>", DetailObj.getString("id"));
		Result += String.format("<p>pass:%s</p><br/>", DetailObj.getString("pass"));
		Result += String.format("<p>mobile:%s</p><br/>", DetailObj.getString("mobile"));
		Result += String.format("<p>mobileStatus:%s</p><br/>", DetailObj.getString("mobileStatus"));
		Result += String.format("<p>email:%s</p><br/>", DetailObj.getString("email"));
		Result += String.format("<p>emailStatus:%s</p><br/>", DetailObj.getString("emailStatus"));
		Result += String.format("<p>title:%s</p><br/>", DetailObj.getString("title"));
		Result += String.format("<p>gender:%s</p><br/>", DetailObj.getString("gender"));
		Result += String.format("<p>lastNameEn:%s</p><br/>", DetailObj.getString("lastNameEn"));
		Result += String.format("<p>firstNameEn:%s</p><br/>", DetailObj.getString("firstNameEn"));
		Result += String.format("<p>lastNameZh:%s</p><br/>", DetailObj.getString("lastNameZh"));
		Result += String.format("<p>firstNameZh:%s</p><br/>", DetailObj.getString("firstNameZh"));
		Result += String.format("<p>status:%s</p><br/>", DetailObj.getString("status"));
		Result += String.format("<p>language:%s</p><br/>", DetailObj.getString("language"));	
		
		response.setCharacterEncoding("utf-8");
		//response.setHeader("Content-type", "text;charset=UTF-8");
		response.getWriter().print(Result);
		response.getWriter().flush();
		response.getWriter().close();
		logger.debug("run ffpsearch.do");
		return null;
	}
	
	@RequestMapping("tiles.do")
	public String tiles(BindingResult request) throws IOException
	{
		
		return "tiles";
	}
	
	@RequestMapping("logout.do")
	public String logout(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		String requestUrl = request.getRequestURL().toString();
		
		String returnURL = request.getScheme() +"://" + request.getServerName() + ":"+
				request.getServerPort() + request.getContextPath() + "/view/test/logout2.do" ;// + request.getServletPath();
		logger.debug(returnURL);
		returnURL = URLEncoder.encode(returnURL);
		
		//System.out.println("*********************LogoutServlet*******************");
		logger.debug("http://www.jdcas.com:8080/cas/logout?service=" + returnURL);
		
		response.sendRedirect("http://www.jdcas.com:8080/cas/logout?service=" + returnURL);
		return null;
	}
	
	@RequestMapping("logout2.do")
	public String logout2(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		
		logger.debug("logout2");
		HttpSession session = request.getSession();
		session.invalidate();
		response.sendRedirect("/SpringTest/index.jsp" );
		return null;
	}
}
